package com.zb.zookeeper;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.apache.zookeeper.KeeperException;
import org.apache.zookeeper.WatchedEvent;
import org.apache.zookeeper.Watcher;
import org.apache.zookeeper.ZooKeeper;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.Iterator;
import java.util.List;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class ZooMonitor {

    private static LinkedBlockingQueue<String> queue = new LinkedBlockingQueue<>();
    private static ZooKeeper zookeeper;
    private static String ROOT = "/dubbo";
    private static final String SEPARATOR = "/";
    private static List<String> childrenList;
    private static final String regEx = "(2[0-4]\\d|25[0-5]|[01]\\d\\d|\\d\\d|\\d)\\.(2[0-4]\\d|25[0-5]|[01]\\d\\d|\\d\\d|\\d)\\.(2[0-4]\\d|25[0-5]|[01]\\d\\d|\\d\\d|\\d)\\.(2[0-4]\\d|25[0-5]|[01]\\d\\d|\\d\\d|\\d)";
    private static Pattern pattern = null;


    static {
        pattern = Pattern.compile(regEx);
    }

    public static void main(String[] args) throws IOException, KeeperException, InterruptedException {
        zookeeper = new ZooKeeper("10.201.3.17:2181", 200000, new Watcher() {
            @Override
            public void process(WatchedEvent event) {
                // TODO: 2017/11/9
            }
        });
        childrenList = zookeeper.getChildren(ROOT, true);
        JSONArray result = new JSONArray();
        childrenList.forEach(o -> {
            JSONObject object = new JSONObject();

            int count = 0;
            System.out.println("=================================================");
            List<String> providers = null;
            try {
                providers = zookeeper.getChildren(ROOT + SEPARATOR + o + SEPARATOR + "providers", true);

                providers.forEach(obj -> {
                    try {
                        JSONArray array = new JSONArray();
                        JSONObject t = new JSONObject();
                        String dec = URLDecoder.decode(obj, "UTF-8");
                        Matcher matcher = pattern.matcher(dec);
                        if (matcher.find()) {
                            String ip = matcher.group();
                            if (object.containsKey(ip)) {
                                array = object.getJSONArray(ip);
                                t.put(array.size()+1,dec);
                                array.add(t);
                                object.put(ip, array);
                                result.add(object);
                            } else {

                                t.put(1, dec);
                                array.add(t);
                                object.put(ip, array);
                            }

                            System.out.println(ip);
                        } else {
                            System.out.println("..........");
                        }
                    } catch (UnsupportedEncodingException e) {
                        e.printStackTrace();
                    }

                });
            } catch (KeeperException | InterruptedException e) {
                e.printStackTrace();
            }

        });

        StringBuilder content = new StringBuilder();

        result.forEach(o->{
            JSONObject json = JSONObject.fromObject(o);
            Iterator iterator = json.keys();
            while (iterator.hasNext()){
                String ip = iterator.next().toString();
                content.append(ip);
                content.append(System.getProperty("line.separator"));
                JSONArray array = json.getJSONArray(ip);
                array.forEach(ooo->{
                    content.append(ooo);
                    content.append(System.getProperty("line.separator"));
                });
            }
        });

        System.out.println(content.toString());

    }


}
